package com.mdb.open.trade.service.product;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdb.bc.database.mybatisplus.SuperService;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.open.trade.common.MdbDataSource;
import com.mdb.open.trade.domain.po.product.Dict;
import com.mdb.open.trade.mapper.product.DictMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类描述：
 *  业务参数字典表 服务类
 *
 * @author liuyi
 * @date 2019-06-29
 */
@Service
@DS(MdbDataSource.PRODUCT)
public class DictService extends SuperService<DictMapper, Dict> {

    public Dict listByCode(String app, String module, String code, String lang) {
        LambdaQueryWrapper<Dict> lambdaQueryWrapper = new LambdaQueryWrapper<Dict>()
                .eq(Dict::getApp, app)
                .like(Dict::getModule, module)
                .eq(Dict::getCode, code);
        if(ObjectUtil.isNotEmpty(lang)) {
            lambdaQueryWrapper.eq(Dict::getLang, lang);
        }
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    public Dict listByCode(String module, String code, String lang) {
        LambdaQueryWrapper<Dict> lambdaQueryWrapper = new LambdaQueryWrapper<Dict>()
                .like(Dict::getModule, module)
                .eq(Dict::getCode, code);
        if(ObjectUtil.isNotEmpty(lang)) {
            lambdaQueryWrapper.eq(Dict::getLang, lang);
        }
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    public Dict listByCode(List<String> modules, String code, String lang) {
        LambdaQueryWrapper<Dict> lambdaQueryWrapper = new LambdaQueryWrapper<Dict>()
                .eq(Dict::getCode, code)
                .and(
                        i -> modules.forEach(m->i.like(Dict::getModule, m).or())
                )
                .eq(Dict::getLang, lang);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    public boolean removeByApp(String app) {
        return this.remove(new QueryWrapper<Dict>().lambda().eq(Dict::getApp, app));
    }
}
